package com.eims.sls_app.ui.adapter;

import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.jar.Attributes.Name;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.android.volley.toolbox.RequestParams;
import com.contans.view.GodFengshenVo.HBL;
import com.contans.view.GodFengshenVo.MZL;
import com.eims.sls_app.dataaccess.Schemes;
import com.eims.sls_app.dataaccess.CircleBean.CommentBean;
import com.eims.sls_app.dataaccess.CircleBean.SchemeinfoBean;
import com.eims.sls_app.dataaccess.CircleBean.TopicBean;
import com.eims.sls_app.dataaccess.utils.HttpUtils;
import com.eims.sls_app.protocol.RspBodyBaseBean;
import com.eims.sls_app.ui.CirclePersonInfo;
import com.eims.sls_app.ui.DocumentaryActivity;
import com.eims.sls_app.ui.LoginActivity;
import com.eims.sls_app.ui.adapter.CircleAdapterMy.GuanzhuAsynTask;
import com.eims.sls_app.ui.adapter.CircleAdapterMy.guanHuMyHandler;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.ImageManager;
import com.eims.sls_app.utils.SnsPopupWindow;

import com.eims.sls_app.view.MyListView2;
import com.eims.sls_app.view.MyToast;
import com.eims.sls_app.view.XCRoundImageView;

import bls.juejin.cn.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class CircleFengshenBodanAdapter extends BaseAdapter {
	private Context context;
	private static final String TAG = "CircleFengshenAdapter";

	private String opt, auth, time, info, imei, crc; // 格式化后的参数
	
	private GuanzhuAsynTask guanzhuAsynTask;
	private guanHuMyHandler gzmHandler;
	private int selectLoctonParse;
	private ArrayList<HBL> hbl;
private String  ganzhu;
	public CircleFengshenBodanAdapter(Context context,ArrayList<HBL> hbl) {
		super();
		this.context = context;
		this.hbl=hbl;
		gzmHandler=new guanHuMyHandler();
	}

	@Override
	public int getCount() {
		if (hbl==null) {
			return 0;
		}

		return hbl.size();
	}

	@Override
	public Object getItem(int position) {

		return position;
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		ViewHolder holder;
		// 判断View是否为空
		if (view == null) {
			holder = new ViewHolder();

			// LayoutInflater inflater = LayoutInflater.from(context);
			// 得到布局文件
			view = LayoutInflater.from(context).inflate(R.layout.adapter_circle_fengsh_item, null);
			holder.iv_Head = (XCRoundImageView) view.findViewById(R.id.iv_item_head);
			holder.tv_name = (TextView) view.findViewById(R.id.nameTv);
			holder.tv_detail = (TextView) view.findViewById(R.id.info);
			holder.tv_Total_amount = (TextView) view.findViewById(R.id.fensheng_tv_top_num);
			holder.tvGuanzhu = (TextView) view.findViewById(R.id.ll_tv_ganzhu);

			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		
		if (position==0) {
			holder.tv_Total_amount.setText("");
			holder.tv_Total_amount.setBackgroundResource(R.drawable.gold);
		}else if (position==1) {
			holder.tv_Total_amount.setText("");
			holder.tv_Total_amount.setBackgroundResource(R.drawable.silver);
		}else if (position==2) {
			holder.tv_Total_amount.setText("");
			holder.tv_Total_amount.setBackgroundResource(R.drawable.copper);
		}else {
			holder.tv_Total_amount.setText(position+1+"");
			holder.tv_Total_amount.setBackgroundResource(R.color.white);
		}
		
		final String name = hbl.get(position).getName();
		// String name = topicBeans.get(position).name;
		final String isname;
		if (isMobileNO(name)) {
			isname = name.substring(0, 3) + "****" + name.substring(7, name.length());
			// holder.tv_name.setText());
		} else {
			isname = name;
			// holder.tv_name.setText(name.substring(0, 2)+"**");
		}
		// if (name.contains("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$")) {
		holder.tv_name.setText(isname);
		holder.tv_detail.setText("近15天某一单最高回报："+formateRate(hbl.get(position).getReward())+"倍");
		String stopGuanzhu = hbl.get(position).getInterest();
		if (stopGuanzhu.equals("0")) {
			// holder.tvGuanzhu.setEnabled(true);
			holder.tvGuanzhu.setText(" +关注 ");
			holder.tvGuanzhu.setTextColor(context.getResources().getColor(R.color.red));
		} else {
			// holder.tvGuanzhu.setEnabled(false);
			holder.tvGuanzhu.setText("已关注");
			holder.tvGuanzhu.setTextColor(context.getResources().getColor(R.color.gaunzhucolor));
		}
		holder.tvGuanzhu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// Toast.makeText(context, "5556", Toast.LENGTH_SHORT).show();
				info = hbl.get(position).getID()+ "";
				selectLoctonParse = position;
				guanzhuAsynTask = new GuanzhuAsynTask();
				guanzhuAsynTask.execute();
			}
		});
		
		ImageManager.getInstance().displayImage(App.url + hbl.get(position).getHeadUrl(),
				holder.iv_Head, ImageManager.getViewsHeadOptions());
	holder.iv_Head.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Bundle bundle=new Bundle();
				bundle.putString( "userId", hbl.get(position).getID());
				Intent intent=new Intent(context,CirclePersonInfo.class);
				intent.putExtras(bundle);
//				intent.putExtra("userId", topicBeans.get(position).id+"");
				context.startActivity(intent);
				
			}
		});
		return view;
	}

	public boolean isMobileNO(String mobiles) {
		Pattern p = Pattern.compile("1[0-9]{10}");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}
	/** 静态类 */
	static class ViewHolder {
		ImageView iv_Head;
		TextView tv_name;
		TextView tv_detail;// 赔率
		TextView tv_Total_amount;// 排序

		TextView tvGuanzhu;
	}

	// 关注
	class GuanzhuAsynTask extends AsyncTask<Integer, Integer, String> {

		String error = "-11";

		@Override
		protected String doInBackground(Integer... params) {

			opt = "90";
			time = RspBodyBaseBean.getTime();
			imei = RspBodyBaseBean.getIMEI(context);

			// info = RspBodyBaseBean.change_TheSun(schemeId+"", context_Sun);

			// String key = MD5.md5(App.user.getUserPass() + App.MD5_key);
			String key = App.key;
			crc = RspBodyBaseBean.getCrc(time, imei, key, info, App.user.getUid());
			auth = RspBodyBaseBean.getAuth(crc, time, imei, App.user.getUid());
			String[] values = { opt, auth, info };
			Log.i(TAG, "晒单" + RequestParams.convertParams(context, opt, info));
			String result = HttpUtils.doPost(App.names, values, App.path);

			System.out.println("晒单：" + result);
			ganzhu = result;
			//
			// if (result.equals("success")) {
			// error = "10";
			// }

			return error;
		}

		@Override
		protected void onPostExecute(String result) {

			gzmHandler.sendEmptyMessage(Integer.parseInt(result));

			super.onPostExecute(result);
		}
	}

	
	@SuppressLint("HandlerLeak")
	class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case 0:

				MyToast.getToast(context, "点赞成功").show();
				break;
			case 1:
				// MyToast.getToast(context, "重复晒单").show();
				break;
			case -2:

				break;
			case -1:

				break;
			case -11:
				
				break;
			case 10:

				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}

	}

	@SuppressLint("HandlerLeak")
	class guanHuMyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case 0:

				break;
			case 1:
				// MyToast.getToast(context, "重复晒单").show();
				break;
			case -2:

				break;
			case -1:

				break;
			case -11:
				Toast.makeText(context, ganzhu, Toast.LENGTH_SHORT).show();
				if (ganzhu.equals("关注成功")) {
					hbl.get(selectLoctonParse).setInterest("1");
				}else if (ganzhu.equals("取消关注")){
					hbl.get(selectLoctonParse).setInterest("0");
				}
				notifyDataSetChanged();
				break;
			case 10:

				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}

	}
	   public String formateRate(String rateStr){  
	         if(rateStr.indexOf(".") != -1){  
	             //获取小数点的位置  
	             int num = 0;  
	             num = rateStr.indexOf(".");  
	               
	             //获取小数点后面的数字 是否有两位 不足两位补足两位  
	             String dianAfter = rateStr.substring(0,num+1);  
	             String afterData = rateStr.replace(dianAfter, "");  
	             if(afterData.length() < 2){  
	                afterData = afterData + "0" ;  
	             }else{  
	                 afterData = afterData;  
	             }  
	             return rateStr.substring(0,num) + "." + afterData.substring(0,2);  
	          }else{  
	            if(rateStr == "1"){  
	               return "100";  
	            }else{  
	                return rateStr;  
	            }  
	          }  
	    }  

}
